#include "read_boy_confg.h"
#include "read_boy_book.h"
#include "ff.h"

//typedef struct book_readbook{
//     
//}book_readbook_t;
//显示电子书界面

void read_boy_book_show_one_page_on_sd(){
    FIL file;  // 文件对象
    FRESULT res;  // FatFs返回结果
    uint32_t rd_len; 
        
    
    uint8_t str_book[512];
    uint8_t *str;
    res = f_open(&file,  my_read_boy_book.book_name, FA_READ);
    if (res == FR_OK) {
        res = f_lseek(&file, my_read_boy_book.lseek);
        if (res == FR_OK) {
            res = f_read(&file, str_book, 512, &rd_len);
                
        }
        f_close(&file) ; 
    }
    //uint16_t len = 0;
    //uint8_t *str = &my_read_boy_book.book[my_read_boy_book.lseek];
    str = str_book;
    while(1){//line
        while(1){//column;//列
            if(*str < 0x80){//en
                //0d   0a
                if((*str == 0x0d) &&(str[1] == 0x0a)){
                    str+=2;
                    my_read_boy_book.char_num+=2;
                    break;
                    
                }else{
                    if((my_read_boy_book.iX - my_read_boy_book.iX_initial_value) >= (my_read_boy_book.width - my_read_boy_book.en_font_x)){
                        break;
                    }
                    if(str[0] < 0x1f){
                        
                    }else{
                        read_boy_draw_char_en(my_read_boy_book.iX,my_read_boy_book.iY,my_read_boy_book.EN_font, *str,0);
                    }
                    str++;
                    my_read_boy_book.char_num++;
                    my_read_boy_book.iX += my_read_boy_book.en_font_x;
                }
            }else{
                if((my_read_boy_book.iX - my_read_boy_book.iX_initial_value) >= (my_read_boy_book.width - my_read_boy_book.cn_font_x)){
                    break;
                }
                //read_boy_draw_char_CN(my_read_boy_book.iX,my_read_boy_book.iY , my_read_boy_book.CN_font,str);//======================
                char hz[3];
                 hz[0] = str[0];
                hz[1] = str[1];
                hz[2] = 0;
                
                draw_string_for_sd(my_read_boy_book.iX,my_read_boy_book.iY,24,hz);
                str+=2;
                my_read_boy_book.char_num+=2;
                my_read_boy_book.iX += my_read_boy_book.cn_font_x;
            }
            if((my_read_boy_book.char_num + my_read_boy_book.lseek) >= my_read_boy_book.totle){
                my_read_boy_book.book_end_flag = 1;
                my_read_boy_book.iX = my_read_boy_book.iX_initial_value;
                my_read_boy_book.iY = my_read_boy_book.iY_initial_value;//================================
                my_read_boy_book.line = 0;
                return;
            }
            
        }
        my_read_boy_book.line++;
        my_read_boy_book.iY += my_read_boy_book.line_hight;
        my_read_boy_book.iX = my_read_boy_book.iX_initial_value;//============================
        if((my_read_boy_book.iY - my_read_boy_book.iY_initial_value) >  (my_read_boy_book.hight - my_read_boy_book.line_hight)){
            my_read_boy_book.iY = my_read_boy_book.iY_initial_value;//================================
            my_read_boy_book.line = 0;
            break;
        }        
    
    }
    do{
        uint16_t percentage;
        read_boy_fb_fillRect(my_read_boy_book.iX_initial_value + my_read_boy_book.width + 2,
                             my_read_boy_book.iY_initial_value,  3,my_read_boy_book.hight - 3);
        percentage  = my_read_boy_book.lseek  * my_read_boy_book.hight /my_read_boy_book.totle;
        
        read_boy_fb_fillRect(my_read_boy_book.iX_initial_value + my_read_boy_book.width + 0,
                             my_read_boy_book.iY_initial_value + percentage,  7, 3);
    
    }while(0);

}

//=================读取书名和大小=========================================
//my_read_boy_book.sd_current_book_num

void read_boy_book_current_name_and_size(){
    FIL file;  // 文件对象
    FRESULT res;  // FatFs返回结果
    FILINFO fno;  // 文件信息结构体
    char out_buff[128];
    uint32_t rd_len;                                
                                    
    res = f_open(&file,  BOOK_PATH_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) { 
        //for(int i = 0; i < my_book.total_count; i++){
            res = f_lseek(&file, my_read_boy_book.sd_current_book_num *128);
            if (res == FR_OK) {
                res = f_read(&file, out_buff, 128, &rd_len);
                strcpy(my_read_boy_book.book_name,"/dzs/"); 
                strcpy(&my_read_boy_book.book_name[5],out_buff); 
            }
        //}    
        
        
    }
    f_close(&file) ; 
    
    //my_book.book_size;
    res = f_open(&file,  my_read_boy_book.book_name, FA_READ);
    if (res == FR_OK) {
        // 获取文件状态
        res = f_stat(my_read_boy_book.book_name, &fno);
        if (res == FR_OK) {
             
            my_read_boy_book.totle = fno.fsize ;
        }
    }
    f_close(&file) ; 
}


char read_boy_book_show_book(){//显示电子书界面
    static char flag = 0;
    char key_num;
    while(1){
        key_num = readboy_key_scan();
        switch(flag){
            case 0:
                READ_BOY_BOOK_DISP_CLEAR; 
                my_read_boy_book.sd_current_book_num = my_read_boy_book.new_index;
                read_boy_book_current_name_and_size();
                my_read_boy_book.lseek = 0;
                my_read_boy_book.book_end_flag = 0;
                flag = 1;
            //break;
            case 1:
                draw_string_for_sd_start();
                //read_boy_book_show_one_page();
                //READ_BOY_BOOK_DISP; 
                READ_BOY_BOOK_DISP_CLEAR; 
                read_boy_book_show_one_page_on_sd();
                READ_BOY_BOOK_DISP;
                draw_string_for_sd_end();
                flag = 2;
            break;
            case 2:
                if(key_num == 1){//显示下一页
                    if(my_read_boy_book.book_end_flag){
                        my_read_boy_book.book_end_flag = 0;
                        my_read_boy_book.char_num = 0;
                        flag = 0;
                        return 2;
                    }
                    my_read_boy_book.lseek += my_read_boy_book.char_num;
                    my_read_boy_book.char_num = 0;
                    flag = 1;
                }
                if(key_num == 2){
                    flag = 0;
                    return key_num;
                }
            break;
                
        }
    }
    return 0;
}


void read_boy_book_reading(char key_num){
    static char flag = 0;
    switch(flag){
        case 0:
            flag = 1;
            READ_BOY_BOOK_FULL_PART(1);
            READ_BOY_BOOK_SET_DEPTH(15);     
        //break;
        case 1://显示目录
            //read_boy_book_init_set_Table_of_Contents();
            read_boy_book_show_Table_of_Contents();
            READ_BOY_BOOK_DISP; 
            flag = 2;
        break;
        case 2:
            read_boy_book_current_name_and_size();
            flag = 3;
        break;
        case 3:
            //READ_BOY_BOOK_DISP_CLEAR; 
            draw_string_for_sd_start();
            //read_boy_book_show_one_page();
            //READ_BOY_BOOK_DISP; 
            READ_BOY_BOOK_DISP_CLEAR; 
            read_boy_book_show_one_page_on_sd();
            READ_BOY_BOOK_DISP;
            draw_string_for_sd_end();
            flag = 4;
        break;
        case 4:
            if(key_num == 1){//显示下一页
                my_read_boy_book.lseek += my_read_boy_book.char_num;
                my_read_boy_book.char_num = 0;
                flag = 3;
            }
            
        break;
    
    }
}

















